﻿using SAD_Assignment1.BLO;
using SAD_Assignment1.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SAD_Assignment1.Public
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["active"] != null)
            {
                ActiveAccount();                
            }
            initialComponent();
        }
        private void initialComponent()
        {
            emailValidator.ForeColor = System.Drawing.Color.Red;
            comparePasswords.ForeColor = System.Drawing.Color.Red;            
        }

        protected void RegisterBtn_Command(object sender, CommandEventArgs e)
        {
            if (IsValidEmail()) { return; }
            string email = txtEmail.Text;
            string pass = txtPassword.Text;
            string name = txtName.Text;
            AccountBLO accBLO = new AccountBLO();
            bool success = accBLO.AddNewAccount(email, pass, name,Request.Url.Port);
            if (success)
            {
                Response.Redirect(Resource.Info + "?msgID=1");
            }
            txtError.Text = "Email đã được đăng ký";
        }

        protected void txtEmail_TextChanged(object sender, EventArgs e)
        {
            string email = txtEmail.Text;
            if (email.Length == 0) 
            {
                emailValidator.Visible = false;
                return; 
            }
            string pattern = @"^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$";
            if (!Regex.IsMatch(email, pattern))
            {
                emailValidator.Text = "Email không hợp lệ";
                emailValidator.Visible = true;
                return;
            }
            else
            {
                emailValidator.Visible = false;
            }
            AccountBLO accBLO = new AccountBLO();
            bool isExist = accBLO.IsEmailExist(email);
            if (isExist)
            {
                emailValidator.Text = "Email đã được đăng ký";
                emailValidator.Visible = true;
                return;
            }
        }

        private bool IsValidEmail()
        {
            return emailValidator.Visible;
        }

        private void ActiveAccount()
        {
            string email = Request.Params["email"];
            int id = 0;
            try { id = Int32.Parse(Request.Params["id"]); }
            catch(Exception){}
            AccountBLO accBLO = new AccountBLO();
            bool success = accBLO.ActiveAccount(email, id);
            if (success)
            {
                Response.Redirect(Resource.Info + "?msgID=0");
                return;
            }
            Response.Redirect(Resource.Error);
        }
    }
}